<?php
class apiActions extends BaseAdminActions
{
	
	public function __construct(){		
		
		
		parent::__construct();
	}
	/**
	 * 
	 * kindeditor.js 上传图片
	 */
	public function actionsKindupload()
	{
	
		//文件保存目录路径
		$save_path = $_SERVER['DOCUMENT_ROOT'].ZSM_WEB_DIR.'upload/images/';
		//文件保存目录URL
		$save_url = ZSM_WWW_HOST . 'upload/images/';
		//定义允许上传的文件扩展名
		$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
		//最大文件大小
		$max_size = 1000000;
		
		$save_path = realpath($save_path) . '/';
		
		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$file_name = $_FILES['imgFile']['name'];
			//服务器上临时文件名
			$tmp_name = $_FILES['imgFile']['tmp_name'];
			//文件大小
			$file_size = $_FILES['imgFile']['size'];
			//检查文件名
			if (!$file_name) {
				alert("请选择文件。");
			}
			//检查目录
			if (@is_dir($save_path) === false) {
				alert("上传目录不存在。");
			}
			//检查目录写权限
			if (@is_writable($save_path) === false) {
				alert("上传目录没有写权限。");
			}
			//检查是否已上传
			if (@is_uploaded_file($tmp_name) === false) {
				alert("临时文件可能不是上传文件。");
			}
			//检查文件大小
			if ($file_size > $max_size) {
				alert("上传文件大小超过限制。");
			}
			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $ext_arr) === false) {
				alert("上传文件扩展名是不允许的扩展名。");
			}
			//创建文件夹
			$ymd = date("Ymd");
			
			$save_path .= $ymd . "/";
			$save_url .= $ymd . "/";
			if (!file_exists($save_path)) {
				mkdir($save_path);
			}
			//新文件名
			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			if (move_uploaded_file($tmp_name, $file_path) === false) {
				alert("上传文件失败。");
			}
			@chmod($file_path, 0644);
			$file_url = $save_url . $new_file_name;
			
			//alert($file_path);
			header('Content-type: text/html; charset=UTF-8');			
			echo json_encode(array('error' => 0, 'url' => $file_url));
			exit;
		}
		
		
	}
	public function actionsCutimg()
	{
	    $thumbid = Http::get('thumbid');
	    if (!$thumbid){
	        $thumbid='thumb';
	    }
		if (Http::get('do')=='post'){
			
			$targ_w = Http::post('w');
			$targ_h =  Http::post('h');
			
			$targ_x = Http::post('x');
			$targ_y = Http::post('y');
			
			$tw = Http::post('tw');
			$th = Http::post('th');
			
			$pw = Http::post('pw');
			$ph = Http::post('ph');
			
			if ($tw>$pw and $th>$ph){
				$wdem = $tw-$pw;
				$hdem = $th-$ph;
				if ($wdem>$hdem){					
					$targ_x = $targ_x*($tw/$pw);
					$targ_y = $targ_y*($tw/$pw);
					
					$targ_w = $targ_w*($tw/$pw);
					$targ_h = $targ_h*($tw/$pw);
					
				}else{
					$targ_x = $targ_x*($th/$ph);
					$targ_y = $targ_y*($th/$ph);

					$targ_w = $targ_w*($th/$ph);
					$targ_h = $targ_h*($th/$ph);
				}
			}else{
				if ($tw>$pw or $th>$ph){
					if ($tw>$pw){
						$targ_x = $targ_x*($tw/$pw);
						$targ_y = $targ_y*($tw/$pw);
						
						$targ_w = $targ_w*($tw/$pw);
						$targ_h = $targ_h*($tw/$pw);
					}else{
						$targ_x = $targ_x*($th/$ph);
						$targ_y = $targ_y*($th/$ph);
	
						$targ_w = $targ_w*($th/$ph);
						$targ_h = $targ_h*($th/$ph);
					}
				}
			}
			 
			
			$jpeg_quality = 90;
			$fileTypeArr = array('jpg','jpeg','gif','png','bmp');
			
			$src =$_POST['imgsrc'];
			$pattern = '/^http:/';			
			if (preg_match($pattern, $src)){
				$src = $_POST['imgsrc'];
				$postPath = $src;
				$filePath = "upload/images/".date("Ymd").'/'.date("YmdHis").rand(10, 10000);
				
				$imgpath = str_replace('.thumb.jpg','',$_SERVER['DOCUMENT_ROOT'].ZSM_WEB_DIR.$filePath);
				
			}else{
				$src = ZSM_WEB_DIR.$_POST['imgsrc'];
				$postPath = str_replace('//','/',$_SERVER['DOCUMENT_ROOT'].$src);
				$imgpath = str_replace('.thumb.jpg','',$postPath);
				if (strlen(ZSM_WEB_DIR)>1){
					$filePath = str_replace(ZSM_WEB_DIR,'',$src);
				}else {
					$filePath = str_replace('//','',$src);
				}
			}			
			
			$typeArr = explode('.', $src);
			$fileTpye = strtolower($typeArr[(count($typeArr)-1)]);
			if (in_array($fileTpye, $fileTypeArr)){				
				$filePath = str_replace('.thumb.'.$fileTpye,'',$filePath).'.thumb.'.$fileTpye;
						
				if ($fileTpye=='jpg' or $fileTpye=='jpeg'){
					$img_r = imagecreatefromjpeg($postPath);
				}				
				if ($fileTpye=='gif'){
					$img_r = imagecreatefromgif($postPath);
				}				
				if ($fileTpye=='png'){
					$img_r = imagecreatefrompng($postPath);
				}				
				if ($fileTpye=='bmp'){
					$img_r = imagecreatefromwbmp($postPath);
				}				
				$dst_r = ImageCreateTrueColor(  Http::post('w'),  Http::post('h') );		
				imagecopyresampled($dst_r,$img_r,0,0, $targ_x, $targ_y, Http::post('w'),  Http::post('h'), $targ_w,$targ_h);
				
				
				imagejpeg($dst_r,$imgpath.'.thumb.jpg',$jpeg_quality);
				//die();
				//header('Content-type: image/jpeg');
				echo '<script>
				parent.setThumbImg( "'.$filePath.'" ,"'.$thumbid.'");
				parent.closeWin();
				</script>';
			}
			die();
		}else{
			//http://comic.hangzhou.com.cn/images/20050101/050701ktmnxse009.jpg
			$img = Http::get('imgsrc');
			$pattern = '/^http:/';
			if (preg_match($pattern, $img)){
				$img = Http::get('imgsrc').'?t='.time();
			}else{
				$img =  ZSM_WWW_HOST.Http::get('imgsrc').'?t='.time();
			}
			
			$this->assign('img', $img);
		}
	}
	public function actionsRelationrs()
	{
		$columns = Http::get('columns');
		$title = Http::get('title');
		Files::factory('News');
		$news = new News();
		$rs = $news->getNewsList($columns,$title);
		$this->assign('rs', $rs);		
		
	}
	/**
	 * 
	 * 搜索相关新闻
	 */
	public function actionsRelation()
	{
		Files::factory('System');
		$news = new System();
		$this->assign('columns', $news->getColumnsList());
	}
	/**
	 * 搜索系统中文章并添加到区块中
	 */
	public function actionsArticle()
	{
		Files::factory('System');
		$news = new System();
		$this->assign('columns', $news->getColumnsList());
	}
	public function actionsArticles()
	{
		$columns = Http::get('columns');
		$title = Http::get('title');
		Files::factory('News');
		$news = new News();
		$rs = $news->getNewsList($columns,$title);
		$this->assign('rs', $rs);	
	}
	
	public function actionsSitemapxml()
	{
		Files::factory('System');
		$sys = new System();
		$sys->createSitemap('baidu');
		$sys->createSitemap('google');
		
		$sys->createSitemapList('google');
		$sys->createSitemapList('baidu');
		echo json_encode(array('html'=>'生成完成'));
		die();
	}
    public function actionsSysimg()
    {
        
        
        $file=new Files();
        $dir=ZSM_ROOT_DIR.DIRECTORY_SEPARATOR.'img/tennis';
        $arr =$file->getSysImg($dir);
        
        $dir=ZSM_ROOT_DIR.DIRECTORY_SEPARATOR.'upload/images';
        $arr = $file->getSysImg($dir,true);
        
        $this->addJs(ZSM_WWW_HOST.'js/jquery/jquery.lazyload.mini.js');
        $this->assign('arr', $arr);  
        
    }
    
    
}#classEnd